package joindemo.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.validation.Valid;
import joindemo.annotation.Log;
import joindemo.common.ResponseResult;
import joindemo.common.enums.BusinessType;
import joindemo.common.enums.HttpStatusEnum;
import joindemo.domain.dto.ArticleDraftRequest;
import joindemo.domain.dto.ArticleDto;
import joindemo.domain.query.ArticlePageQuery;
import joindemo.domain.query.ArticleSearchQuery;
import joindemo.domain.vo.ArticleVo;
import joindemo.domain.vo.PageVo;
import joindemo.service.IArticleService;
import joindemo.utils.UserHelper;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 文章控制器
 * @author pym
 */
@RestController
@RequestMapping("/article")
@RequiredArgsConstructor
@Validated
public class ArticleController {

    private final IArticleService articleService;

    @Operation(summary = "获取文章")
    @Log(title = "获取文章详情", businessType = BusinessType.SELECT)
    @GetMapping("/{id}")
    public ResponseResult<ArticleVo> getArticle(@PathVariable Integer id) {
        ArticleVo articleVo = articleService.getArticleById(id);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "获取文章成功", articleVo);
    }

    @Operation(summary = "分页查询文章")
    @Log(title = "分页查询文章", businessType = BusinessType.SELECT)
    @GetMapping("/page")
    public ResponseResult<PageVo<ArticleVo>> pageArticles(@Valid ArticlePageQuery query) {
        PageVo<ArticleVo> pageData = articleService.pageArticles(query);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "查询文章列表成功", pageData);
    }

    @Operation(summary = "搜索文章")
    @Log(title = "搜索文章", businessType = BusinessType.SELECT)
    @GetMapping("/search")
    public ResponseResult<PageVo<ArticleVo>> searchArticles(@Valid ArticleSearchQuery query) {
        PageVo<ArticleVo> searchResult = articleService.searchArticles(query);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "搜索文章成功", searchResult);
    }

    @Operation(summary = "添加文章")
    @Log(title = "添加文章", businessType = BusinessType.INSERT)
    @SaCheckLogin
    @PostMapping
    public ResponseResult<ArticleVo> addArticle(@Valid @RequestBody ArticleDto articleDto) {
        Long userId = UserHelper.getUserId();
        String userName = UserHelper.getUserName();
        ArticleVo articleVo = articleService.addArticle(articleDto, userId, userName);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "添加文章成功", articleVo);
    }

    @Operation(summary = "更新文章")
    @Log(title = "更新文章", businessType = BusinessType.UPDATE)
    @SaCheckLogin
    @PutMapping("/{id}")
    public ResponseResult<ArticleVo> updateArticle(@PathVariable Integer id, @Valid @RequestBody ArticleDto articleDto) {
        ArticleVo updatedVo = articleService.updateArticle(id, articleDto, UserHelper.getUserId());
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "更新文章成功", updatedVo);
    }

    @Operation(summary = "删除文章")
    @Log(title = "删除文章", businessType = BusinessType.DELETE)
    @SaCheckLogin
    @DeleteMapping("/{id}")
    public ResponseResult<Void> deleteArticle(@PathVariable Integer id) {
        articleService.deleteArticle(id, UserHelper.getUserId());
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "删除文章成功", null);
    }

    @Operation(summary = "保存草稿")
    @Log(title = "保存草稿", businessType = BusinessType.INSERT)
    @SaCheckLogin
    @PostMapping("/drafts")
    public ResponseResult<String> saveDraft(@Valid @RequestBody ArticleDraftRequest request) {
        String draftId = articleService.saveDraft(UserHelper.getUserId(), request.getDraftId(), request.getArticle());
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "保存草稿成功", draftId);
    }

    @Operation(summary = "获取草稿")
    @Log(title = "获取草稿", businessType = BusinessType.SELECT)
    @SaCheckLogin
    @GetMapping("/drafts/{draftId}")
    public ResponseResult<ArticleDto> getDraft(@PathVariable String draftId) {
        ArticleDto draft = articleService.getDraft(UserHelper.getUserId(), draftId);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "获取草稿成功", draft);
    }

    @Operation(summary = "删除草稿")
    @Log(title = "删除草稿", businessType = BusinessType.DELETE)
    @SaCheckLogin
    @DeleteMapping("/drafts/{draftId}")
    public ResponseResult<Void> deleteDraft(@PathVariable String draftId) {
        articleService.deleteDraft(UserHelper.getUserId(), draftId);
        return ResponseResult.success(HttpStatusEnum.SUCCESS.getCode(), "删除草稿成功", null);
    }
}